package ch.subgate.spritestudio.types;

import java.util.ArrayList;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ColorAdapter extends BaseAdapter {
	
	private Context mContext;
	
	private ArrayList<Integer> mColors;
	
	private int mViewSize;
	
	public ColorAdapter(Context context, ArrayList<Integer> colors, int displayDPI) {
		mContext = context;
		mColors = colors;
		
		switch (displayDPI) {
		case DisplayMetrics.DENSITY_LOW:
			mViewSize = 36;
			break;
		case DisplayMetrics.DENSITY_MEDIUM:
			mViewSize = 48;
			break;
		case DisplayMetrics.DENSITY_HIGH:
			mViewSize = 72;
			break;
		case 320:
			mViewSize = 96;
			break;
		}
	}
	
	@Override
	public int getCount() {
		if (mColors != null) {
			return mColors.size();
		}
		
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (mColors != null) {
			return mColors.get(position);
		}
		
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView view;
		if (convertView == null) {
			view = new ImageView(mContext);
			view.setLayoutParams(new GridView.LayoutParams(mViewSize, mViewSize));
			view.setScaleType(ImageView.ScaleType.CENTER_CROP);
			view.setPadding(0, 0, 0, 0);
		}
		else {
			view = (ImageView)convertView;
		}
		if (mColors != null) {
			view.setBackgroundColor(mColors.get(position).intValue());
		}
		
		return view;
	}
	
	public void setColors(ArrayList<Integer> colors) {
		mColors = colors;
	}
	
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		mColors = null;
	}

}
